<template>
  <div class="page">
    <v-pageTitle title="加班汇总"></v-pageTitle>
    <imp-panel>
      <v-pageSearch v-if="hasPermission('查询')">
        <el-row style="width: 100%;">
          <el-form :inline="true">
            <el-col :span="24">
              <el-form-item label="时间">
                    <el-date-picker
                      size="small"
                      v-model="formSearch.collectYear"
                      type="year"
                      placeholder="选择时间"
                      >
                    </el-date-picker>
                  </el-form-item>
              <el-form-item label="部门">
                <el-select size="small" v-model="formSearch.departmentId" placeholder="请选择">
                  <el-option
                    v-for="item in department_options"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="员工">
                <el-select size="small" v-model="formSearch.userId" filterable placeholder="请输入查询关键字">
                  <el-option
                    v-for="item in user_options"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button size="small" type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </v-pageSearch>
    
      <v-pageTable pagination :sizeChange="sizeChange" :pageChange="pageChange" :paginationTotal="paginationTotal" paginationAlign="right">
        <el-table size="small" v-loading="table.loading" stripe
          :max-height="table.autoHeight"
          :data="table.body" style="width: 100%" @selection-change="selsChange" 
          :header-row-style='headerBg' :header-cell-style="headerBg"
          :default-sort="{prop: 'date', order: 'descending'}">
          <el-table-column prop="collect_year" label="年份" min-width="100" sortable>
          </el-table-column>
          <el-table-column prop="name" label="员工" min-width="100" sortable>
          </el-table-column>
          <el-table-column prop="sum_overtime" label="加班时间" min-width="150" sortable>
          </el-table-column>
          <el-table-column prop="sum_casual_leave" label="请假时间" min-width="150" sortable>
          </el-table-column>
          <el-table-column prop="total_year" label="总计" min-width="150" sortable>
          </el-table-column>
          <el-table-column label="操作" width="120">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-more"
                v-if="hasPermission('明细')"
                @click="handleEdit(scope.row)">明细
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </v-pageTable>
    </imp-panel>

    <!--明细信息界面-->
    <el-dialog title="明细" :visible.sync="editFormVisible" @close="dialogClose" width="60%" :close-on-click-modal="false">
      <el-table size="small" v-loading="table_dialog.loading" stripe
        :data="table_dialog.body" style="width: 100%" 
        :header-row-style='headerBg' :header-cell-style="headerBg"
        :default-sort="{prop: 'date', order: 'descending'}">
        <el-table-column prop="collect_time" label="月份" min-width="100" sortable>
        </el-table-column>
        <el-table-column prop="name" label="员工" min-width="100" sortable>
        </el-table-column>
        <el-table-column prop="overtime_hours" label="当月加班时间" min-width="120" sortable>
        </el-table-column>
        <el-table-column prop="casual_leave_hours" label="请假时间" min-width="100" sortable>
        </el-table-column>
        <el-table-column prop="history_overtime_hours" label="历史加班时间" min-width="120" sortable>
        </el-table-column>
        <el-table-column prop="month_total" label="当月加班总计" min-width="120" sortable>
        </el-table-column>
        <el-table-column prop="remain_hours" label="加班总计" min-width="100" sortable>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>
<script>
import { mapState, mapMutations, mapActions } from 'Vuex'
import panel from "@/components/common/panel.vue"
import util from '@/util/util'
export default {
  components: {
    'imp-panel': panel
  },
  data() {
    return {
      editFormVisible:false,  //编辑弹窗是否显示
      department_options: [],
      user_options: [],
      table: {
          loading: true,
          body: [],
          autoHeight: 634,
          page: 1,
          pageSize: 10
      },
      formSearch:{
        collectYear:new Date(),
        departmentId: -1,
        userId: ''
      },
      paginationTotal: 0,
      sels:[],

      // 弹框表格数据
      table_dialog: {
          loading: true,
          body: []
      }
    }
  },
  methods: {
    //基础数据获取
    baseInfo(){
      let this_ = this;
      let department_url = 'base/baseDept/getAllDeps';
      // 获取部门
      util.httpJesen(department_url,'',function(data){
        data.data.unshift({"name":"全部","id":-1});
        this_.department_options = data.data;
      });

      // 获取用户
      let user_url = 'base/baseUser/getBaseUsersByKeywords'
      let user_params = {
        "companyId":"",
        "name":"",
        "pageSize": 500,
        "page": 1,
        "statusValue": 0,
        usercode: ""
      };
      util.httpJesen(user_url,user_params,function(data){
          this_.user_options = data.data.records;
      });
    },
    //编辑当前数据
    handleEdit: function(row){
      this.editFormVisible = true;
      let this_ = this;
      this_.table_dialog.loading = true;
      let url = 'attendance/summaryDetail';
      
      let params = {
        "collectYear": row.collect_year,
        "departmentId": row.department_id,
        "userId": row.user_id
      };
      util.httpJesen(url,params,function(data){
          this_.table_dialog.body = data.data;
          this_.table_dialog.loading = false;
      });
    },
    //编辑界面close 关闭的回调
    dialogClose(){
      
    },
    // 监控分页数据变化
    currentPage(data){
      this.$store.commit("currentPage", data)
    },
    headerBg: function(row){
      return { "background": "#EEF1F6","color":"#1F2D3D" };
    },
    // 导出
    doExport(){
      let params = {
        "mapSearch":{
          "id": this.formSearch.id,
          "parentId": this.formSearch.parentId
        },
        "type": 'ExportChargeItem'
      };
      let this_ = this;
      let paramsStr = JSON.stringify(params);
      window.location.href = "/api/export/do?data="+encodeURI(paramsStr);
    },
    onSearch(){
      this.pageChange(1,'page')
      this.currentPage(1)
    },
    getTableData(val,flag) {
      // ajax 查询表格数据
      let this_ = this;
      this_.table.loading = true;
      let url = 'attendance/summaryList';
      if(flag==='pageSize'){
        this.table.pageSize = val;
      }else if(flag==='page'){
        this.table.page = val;
      };
      
      let params = {
        "collectYear": util.formatDate.format(new Date(this.formSearch.collectYear), 'yyyy'),
        "departmentId":this.formSearch.departmentId,
        "userId":this.formSearch.userId==''? -1 : this.formSearch.userId,
        "page":this.table.page,
        "pageSize":this.table.pageSize
      };
      util.httpJesen(url,params,function(data){
          this_.table.body = data.data.records;
          this_.paginationTotal = data.data.total;
          this_.table.loading = false;
      });
    },
    sizeChange(val){
      this.getTableData(val,'pageSize')
    },
    pageChange(val){
      this.getTableData(val,'page')
      this.currentPage(val)
    },
    selsChange: function (sels) {
      console.log(sels)
      this.sels = sels;
    },
  },
  mounted() {
    // 获取table数据
    this.onSearch();
    this.baseInfo();


    //监控浏览器高度 设置table最大高度
    let this_ = this;
    let clientHeight = document.body.clientHeight; 
    this_.table.autoHeight = clientHeight-260;
    window.addEventListener('resize', function(){
      let clientHeight = document.body.clientHeight; 
      this_.table.autoHeight = clientHeight-2860;
    })
  },
  beforeDestory(){

  }
}
</script>
<style lang="scss" scoped>

</style>
